package com.sun.jvm;

import org.openjdk.jol.info.ClassLayout;

/**
 * 查看对象大小
 */
public class ObjectSizeTest {

    public static void main(String[] args) {

        /**
         * java.lang.String object internals:
         * OFF  SZ     TYPE DESCRIPTION               VALUE
         *   0   8          (object header: mark)     0x0000000000000001 (non-biasable; age: 0)
         *   8   4          (object header: class)    0xf80002da
         *  12   4   char[] String.value              [h, e, l, l, o,  , w, o, r, l, d]
         *  16   4      int String.hash               0
         *  20   4          (object alignment gap)
         * Instance size: 24 bytes
         * Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
         */
        String str = "hello world222222222";
        System.out.println(ClassLayout.parseInstance(str).toPrintable());

        /**
         * [I object internals:
         * OFF  SZ   TYPE DESCRIPTION               VALUE
         *   0   8        (object header: mark)     0x0000000000000001 (non-biasable; age: 0)
         *   8   4        (object header: class)    0xf800016d
         *  12   4        (array length)            10
         *  16  40    int [I.<elements>             N/A
         * Instance size: 56 bytes
         * Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
         */
        int[] arr = new int[10];
        System.out.println(ClassLayout.parseInstance(arr).toPrintable());

        /**
         * java.lang.Object object internals:
         * OFF  SZ   TYPE DESCRIPTION               VALUE
         *   0   8        (object header: mark)     0x0000000000000001 (non-biasable; age: 0)
         *   8   4        (object header: class)    0xf80001e5
         *  12   4        (object alignment gap)
         * Instance size: 16 bytes
         * Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
         */
        Object object = new Object();
        System.out.println(ClassLayout.parseInstance(object).toPrintable());
    }
}
